package com.warren.auth.handler;

import com.warren.auth.utils.JSONAuthentication;
import com.warren.response.Result;
import com.warren.response.ResultCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author warren
 * @date 2021/3/28 0028 14:57
 * @Description 用来解决匿名用户访问需要权限才能访问的资源时的异常
 **/
@Component("myAuthenticationEntryPoint")
public class JWTAuthenticationEntryPoint extends JSONAuthentication implements AuthenticationEntryPoint {

    /**
     * 当用户尝试访问需要权限才能的rest资源而不提供token或者token过期，将调用此方法
     * @param request
     * @param response
     * @param e
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Result result = Result.error(ResultCode.USER_NOT_LOGIN);

        this.WriteJSON(request,response,result);
    }
}
